package cxt.cn.community.service.impl;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cxt.cn.community.dao.adminMapper;
import cxt.cn.community.dao.buildMapper;
import cxt.cn.community.dao.roomMapper;
import cxt.cn.community.entity.Dto.*;
import cxt.cn.community.entity.User;
import cxt.cn.community.service.RedisService;
import cxt.cn.community.service.adminService;
import cxt.cn.community.utils.FileUploadResource;
import cxt.cn.community.utils.PasswordEncode;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;


/*
 * @Author chenxiaotian
 * @Description:
 * @Date:2022122022/12/30
 * @Time:15:01
 *
 */
@Service
public class adminServiceImpl extends ServiceImpl<adminMapper, User> implements adminService {
    @Autowired
    private adminMapper adminMapper;


    @Autowired
    private buildMapper buildMapper;

    @Autowired
    private roomMapper roomMapper;

    @Autowired
    private RedisService redisService;

    @Autowired
    FileUploadResource fileUploadResource;

    @Override
    public User login(AdminLoginDto cmsLogin) {
        String account = cmsLogin.getUsername();
        String password = cmsLogin.getPassword();
        String s = PasswordEncode.strToMD5(password, 16, true);
        User user = adminMapper.selectOne(new QueryWrapper<User>().lambda().eq(User::getUsername, account));
        if (ObjectUtils.isNotNull(user)&&user.getPassword().equals(s)){
            redisService.set("user", JSON.toJSONString(user));
            return user;
        }
        return null;
    }

    @Override
    public int register(AdminRegisterDto adminRegisterDto) {
        User user1 = adminMapper.selectOne(new QueryWrapper<User>().lambda().eq(User::getUsername, adminRegisterDto.getUsername()));
        if (user1!=null){
            return 0;
        }
        User user = new User();
        user.setUsername(adminRegisterDto.getUsername());
        user.setPassword(PasswordEncode.strToMD5(adminRegisterDto.getPassword(),16,true));
        user.setUserNickname(adminRegisterDto.getNickname());
        if (StringUtils.isEmpty(adminRegisterDto.getUserAvatar())) {
            user.setUserAvatar("");//放置头像地址
        }
        user.setUserAvatar(adminRegisterDto.getUserAvatar());
        user.setUserStatus(1);
        user.setUserSex(adminRegisterDto.getUsersex());
        user.setUserBuildId(adminRegisterDto.getBuild());
        user.setUserRoomId(adminRegisterDto.getRoom());
        return adminMapper.insert(user);
    }


    @Override
    public int modify(ModifyDto modifyDto) {
        String username = modifyDto.getUsername();
        String password = modifyDto.getPassword();
        String s = PasswordEncode.strToMD5(password, 16, true);
        UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("username",username).set("password",s);
        return adminMapper.update(null, updateWrapper);
    }

    @Override
    public int modifyUserInfo(AdminInfoDto adminInfoDto) {
        User user = new User();
        user.setUserAvatar(adminInfoDto.getUserAvatar());
        user.setUserSex(adminInfoDto.getUsersex());
        user.setUserRoomId(adminInfoDto.getRoom());
        user.setUserBuildId(adminInfoDto.getBuild());
        user.setUserNickname(adminInfoDto.getNickname());
        user.setUsername(adminInfoDto.getUsername());
        user.setUserStatus(1);
        UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("username",adminInfoDto.getUsername());
        redisService.delete("user");
        redisService.set("user",JSON.toJSONString(user));
        return  adminMapper.update(user,updateWrapper);
    }

    @Override
    public int modifyPassword(ModifyPassword modifyPassword) {
        String oldPassword = modifyPassword.getOldPassword();
        String username = modifyPassword.getUsername();
        User user = adminMapper.selectOne(new QueryWrapper<User>().lambda().eq(User::getUsername, modifyPassword.getUsername()));
        if (user.getPassword().equals(PasswordEncode.strToMD5(oldPassword,16,true))){
            UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("username",username).set("password",PasswordEncode.strToMD5(modifyPassword.getNewPassword(),16,true));
            redisService.delete("user");
            User user1 = adminMapper.selectOne(new QueryWrapper<User>().lambda().eq(User::getUsername, modifyPassword.getUsername()));
            redisService.set("user",JSON.toJSONString(user1));
            return adminMapper.update(null,updateWrapper);
        }
        return 0;
    }

    @Override
    public void logout(String username) {
        String user = redisService.get("user");
        JSONObject jsonObject = JSONUtil.parseObj(user);
        User user1 = JSONUtil.toBean(jsonObject, User.class);
        if (user1.getUsername().equals(username)){
            redisService.delete("user");
        }
    }


}
